package com.ubst.smartad.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.ubst.smartad.dao.TSALogonDO;
import com.ubst.smartad.dao.TSAUserAccountDO;
import com.ubst.smartad.logger.Debug;

public class TSALogonMngImpl {
	
	Connection con = null;
	private String serverName;
	private Debug debug;
	
	/** 
     * 생성자
     *   
    */ 
	public TSALogonMngImpl(Connection con, String serverName, String log_level) {
		this.con = con;
		this.serverName = serverName;
		this.debug = new Debug("TSALogonMngImpl", log_level);
	}
	
	/**
	 * 로그인세션  정보 등록
	 * @param userAccountDO
	 * @throws SQLException
	 */
	public void insertLogon(TSALogonDO logonDO) throws SQLException {
		PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        
        try {
            queryBuf = new StringBuffer();
			queryBuf.append("INSERT INTO TSA_LOGON " )
				.append(" ( ")
				.append("  SESSIONID ,SYSTEMID ,USERID ,REMOTEIP ,ACCOUNTID ")
				.append(" ,LOGONFLAG ,LOGINDATE ,LOGOUTDATE ")
				.append(" ) ")
				.append(" VALUES")
				.append(" ( ") 
				.append("  ? ,? ,? ,? ,? ")
				.append(" ,? ,? ,? ")
				.append(" ) ");

			debug.debug("insertLogon ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, logonDO.sessionid);
			pstmt.setString(idx++, logonDO.systemid);
			pstmt.setString(idx++, logonDO.userid);
			pstmt.setString(idx++, logonDO.remoteip);
			pstmt.setString(idx++, logonDO.accountid);
			pstmt.setString(idx++, logonDO.logonflag);
			pstmt.setString(idx++, logonDO.logindate);
			pstmt.setString(idx++, logonDO.logoutdate);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;
        } catch (SQLException e){
			throw e;
        } finally {
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }
	}
	
	/**
	 * 로그인세션  정보 수정
	 * @param userAccountDO
	 * @throws SQLException
	 */
	public void updateLogon(TSALogonDO logonDO) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {
            queryBuf = new StringBuffer();
			queryBuf.append("UPDATE TSA_LOGON " )
				.append(" SET ")
				.append("  SESSIONID=? ,SYSTEMID=? ,USERID=? ,REMOTEIP=? ,ACCOUNTID=? ")
				.append(" ,LOGONFLAG=? ,LOGINDATE=? ,LOGOUTDATE=? ")
				.append(" WHERE SESSIONID=? ");

			debug.debug("updateLogon ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, logonDO.sessionid);
			pstmt.setString(idx++, logonDO.systemid);
			pstmt.setString(idx++, logonDO.userid);
			pstmt.setString(idx++, logonDO.remoteip);
			pstmt.setString(idx++, logonDO.accountid);
			pstmt.setString(idx++, logonDO.logonflag);
			pstmt.setString(idx++, logonDO.logindate);
			pstmt.setString(idx++, logonDO.logoutdate);
			pstmt.setString(idx++, logonDO.sessionid);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }
	
	/**
	 * 로그인세션  정보 목록 조회
	 * @param userid
	 * @return
	 * @throws SQLException
	 */
	public TSALogonDO selectLogon(String sessionid) throws SQLException {

		PreparedStatement pstmt=null;
		ResultSet rs = null;

		TSALogonDO logonDO = null;

		try {
			
			StringBuffer queryBuf = new StringBuffer();
			queryBuf.append("SELECT ")
				.append("  SESSIONID ,SYSTEMID ,USERID ,REMOTEIP ,ACCOUNTID ")
				.append(" ,LOGONFLAG ,LOGINDATE ,LOGOUTDATE ")
				.append(" FROM TSA_LOGON ")
				.append(" WHERE SESSIONID=? ");

			debug.debug("selectLogonList ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, sessionid);

			rs = pstmt.executeQuery();

			while ( rs.next() ){
				logonDO = new TSALogonDO();
				logonDO.sessionid = sessionid;
				logonDO.systemid = rs.getString("SYSTEMID");
				logonDO.userid = rs.getString("USERID");
				logonDO.remoteip = rs.getString("REMOTEIP");
				logonDO.accountid = rs.getString("ACCOUNTID");
				logonDO.logonflag = rs.getString("LOGONFLAG");
				logonDO.logindate = rs.getString("LOGINDATE");
				logonDO.logoutdate = rs.getString("LOGOUTDATE");
			}

			rs.close();
			pstmt.close();

		}catch (SQLException e){
			throw e;
		}finally{
			if ( rs != null) try{rs.close();}catch(SQLException ee){}
			if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
		}

		return logonDO;
	}
	
	/**
	 * 로그인세션  정보 삭제
	 * @param userid
	 * @throws SQLException
	 */
	public void deleteLogon(String sessionid) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

            queryBuf = new StringBuffer();
			queryBuf.append("DELETE FROM TSA_LOGON " )
				.append(" WHERE SESSIONID=? ");

			debug.debug("deleteLogon ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, sessionid);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }
}
